<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
         <!-- <el-col :lg="6" :md="12">
            <el-form-item label="专业名称:">
              <el-input
                clearable
                v-model="where.name"
                placeholder="请输入专业名称"/>
            </el-form-item>
          </el-col> -->
          <el-col :lg="6" :md="12">
            <el-form-item label="统计学年:">
              <el-select
                filterable
                clearable
                v-model="where.annual"
                @change="$forceUpdate()"
                size="small"
                placeholder="-学年统计-"
                class="ele-block">
                <el-option v-for="item in xuenianList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="10" :md="12">
            <el-form-item label="统计日期:">
              <el-col :lg="8" :md="12">
                <el-date-picker
                  type="date"
                  class="ele-fluid"
                  v-model="where.startTime"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  placeholder="请选择开始日期"/>
              </el-col>
              <el-col :lg="2" :md="12">
                至
              </el-col>
              <el-col :lg="8" :md="12">
                <el-date-picker
                  type="date"
                  class="ele-fluid"
                  v-model="where.endTime"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  placeholder="请选择结束日期"/>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="模式:">
              <el-select
                clearable
                v-model="where.hasTest"
                placeholder="请选择模式"
                class="ele-fluid">
                <el-option label="测试模式" value="1"/>
                <el-option label="训练模式" value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="项目:">
              <project-select  v-model="where.projectId" @chageProject="chageProject"  @chageProjectId="chageProjectId"></project-select>
            </el-form-item>
          </el-col>

           <el-col :lg="6" :md="12">
             <el-form-item ref="grade" prop="grade" label="年级:">
               <grade-select  v-model="where.gradeId" @changeGrade="changeGrade" @changeGradeId="changeGradeId"></grade-select>
             </el-form-item>
           </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="院系类型:">
              <el-select
                clearable
                v-model="where.stype"
                placeholder="请选择院系类型"
                @change="getDepartmentsList"
                class="ele-fluid">
                              <el-option label="自然院系" value="1"/>
                              <el-option label="体育院系" value="2"/>
                            </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="院系:">
              <el-select
                filterable
                clearable
                v-model="where.departmentsId"
                @change="$forceUpdate();getProfessionalList()"
                size="small"
                placeholder="-请选择院系-"
                class="ele-block">
                <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="专业:" prop="professional">
                <el-select
                  clearable
                  v-model="where.professionalId"
                  @change="$forceUpdate();getClassList()"
                  size="small"
                  placeholder="-请选择专业-"
                  class="ele-block">
                  <el-option v-for="item in professionalList" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
            </el-form-item>
          </el-col>

                                  <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        v-if="showtable"
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
<template slot="toolbar">
            <el-button
              @click="exportData"
              type="primary"
              icon="el-icon-download"
              class="ele-btn-icon"
              size="small"
             >导出
            </el-button>
          </template>
          <!-- 院系类型列 -->
          <template slot="type" slot-scope="{row}">
                      <el-tag v-if="row.type === 1" type="" size="small">自然院系</el-tag>
                      <el-tag v-if="row.type === 2" type="success" size="small">体育院系</el-tag>
                    </template>

          <!-- 状态列 -->
          <template slot="status" slot-scope="{row}">
            <el-switch
              v-model="row.status"
              @change="status(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>


                        </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <profess-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ProfessEdit from './prof-edit';
import gradeSelect from '@/components/gradeSelect'
import projectSelect from '@/components/projectSelect'
import XLSX from 'xlsx';

export default {
  name: 'ExamProfess',
  components: {ProfessEdit,gradeSelect,projectSelect},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      showtable:false,
      // 表格数据接口
      url: '/prof/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'pro_name',
          label: '项目',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'grade_name',
          label: '年级',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'type',
          label: '院系类型',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'type',
        },
        {
          prop: 'depart_name',
          label: '院系名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
              {
          prop: 'name',
          label: '专业名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
         {
           prop: 'number',
           label: '班级人数',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'avg',
           label: '平均分',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'goodNum',
           label: '优秀人数',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'liangNum',
           label: '良好人数',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'jigeNum',
           label: '及格人数',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'noJigeNum',
           label: '不及格人数',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'youRate',
           label: '优良率',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'hegeRate',
           label: '合格率',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      projectList:[],
      xuenianList:[],
      classList: [],
      gradeList:[],
      departmentsList:[],
      professionalList:[],
    };
  },
  created() {

    // 获取学年
    this.getXueList();
    // 获取班级列表
    this.getClassList();
    this.getProfessionalList();
    this.getDepartmentsList();
  },
  methods: {
    chageProjectId(val){
      this.where.projectId = val;
      this.$forceUpdate();
    },
    changeGradeId(val){
      this.where.gradeId = val;
      this.$forceUpdate();
    },
    changeGrade(val){
      this.where.gradeId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
    },
    chageProject(val){
      this.where.projectId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {
        projectId:this.where.projectId,
        gradeId:this.where.gradeId
      };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 导出数据 */
    exportData() {
      let array = [['ID', '项目', '年级', '院系类型', '院系名称','专业名称','班级人数', '平均分', '优秀人数', '良好人数', '及格人数', '不及格人数','优良率','合格率']];
      const loading = this.$loading({lock: true});
      this.$http.get('/prof/index?page=1&limit=999999&projectId='+this.where.projectId+'&gradeId='+this.where.gradeId+'&annual='+this.where.annual+'&startTime='+this.where.startTime
      +'&endTime='+this.where.endTime+'&hasTest='+this.where.hasTest+'&stype='+this.where.stype+'&departmentsId='+this.where.departmentsId+
      '&professionalId='+this.where.professionalId).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.forEach(d => {
            array.push([
              d.id,
              d.pro_name,
              d.grade_name,
              d.etype,
              d.depart_name,
              d.name,
              d.number,
              d.avg,
              d.goodNum,
              d.liangNum,
              d.jigeNum,
              d.noJigeNum,
              d.youRate,
              d.hegeRate,
             // this.$util.toDateString(d.createTime)
            ]);
          });
          this.$util.exportSheet(XLSX, array, '专业锻炼');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /**
     * 获取专业列表
     */
    getProfessionalList() {
      if(this.where.professionalId){
        delete this.where.classId
      }
      this.$http.post('/professional/getProfessionalList',{type: this.where.stype,sid:this.where.departmentsId}).then(res => {
        if (res.data.code === 0) {
          this.professionalList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
      this.getClassList();
    },
    /**
     * 获取院系列表
     */
     getDepartmentsList() {
      if(this.where.stype){
        delete this.where.departmentsId
        delete this.where.professionalId
        delete this.where.classId
        // this.where.departmentsId = '';
        // this.where.professionalId = '';
        // this.where.classId = '';
        // this.$forceUpdate();
      }
      this.$http.post('/departments/getDepartmentsList',{type: this.where.stype}).then(res => {
         if (res.data.code === 0) {
           this.departmentsList = res.data.data;
         } else {
           this.$message.error(res.data.msg);
         }
       }).catch(e => {
         this.$message.error(e.message);
       });
      this.getProfessionalList();
      this.getClassList();
   },
    /**
        * 获取组织列表
        */
       getProjectList() {
         this.$http.get('/project/getProjectList').then(res => {
           if (res.data.code === 0) {
             this.projectList = res.data.data;
             this.where.projectId=this.projectList && this.projectList.length > 0 ? this.projectList[0].id : ''
             return this.where.projectId
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
       getXueList() {
         this.$http.get('/annual/getXueList').then(res => {
           if (res.data.code === 0) {
             this.xuenianList = res.data.data;
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
    /**
        * 获取班级列表
        */
       getClassList() {
         if(this.where.superior){
           this.where.class = null;
           this.$forceUpdate();
         }
         this.$http.post('/clss/getClassList',{yid:this.where.superior,gid:this.where.gradeId,pid:this.where.professionalId,stype:this.where.stype}).then(res => {
           if (res.data.code === 0) {
             this.classList = res.data.data;
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/profess/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的专业体测吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/profess/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status      })
      this.$http.post('/profess/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
                    }
}
</script>

<style scoped>
</style>
